package com.bcd.model;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.bcd.beans.Article;
import com.bcd.beans.ArticleFactory;
import com.bcd.beans.Book;
import com.bcd.beans.CD;
import com.bcd.beans.DVD;

public class ArticleManager {
	
	private DbManager _db;
	private ResultSet _rs;
	private ArrayList<Article> _articleList;
	private Article _article;
	
	public ArticleManager(){
		_db = DbManager.getInstance();		
	}	
	
	public Article GetArticle(int id){	
		_rs = _db.ExecuteSelect("SELECT * FROM articles WHERE article_id = " + id);
		initArticle();
		return _article;
	}
	
	public ArrayList<Article> GetUserArticles(int id){
		_articleList = new ArrayList<Article>();
		_rs = _db.ExecuteSelect("SELECT * FROM articles where Users_user_id = " + id);
		while(initArticle()){
			_articleList.add(_article);
		}
		return _articleList;
	}
	
	public ArrayList<Article> GetArticleList(ArticleType type){
		_articleList = new ArrayList<Article>();
		String sql_param = "";
		ArticleType _type = type; 
		switch(_type){
			case BOOK:
				sql_param = "'BOOK'";
				break;
			case CD:
				sql_param = "'CD'";
				break;
			case DVD:
				sql_param = "'DVD'";
				break;
			default:
				sql_param = "'BOOK' OR article_type = 'CD' OR article_type = 'DVD'";
		}
		
		_rs = _db.ExecuteSelect("SELECT * FROM articles WHERE article_type = " + sql_param);
		
		while(initArticle()){
			_articleList.add(_article);
		}
		return _articleList;
	}
	
	private boolean initArticle(){
		try{
			if(_rs.next()){
				createArticle();
				_article.setArticle(_rs);
				return true;
			}			
		}catch(SQLException e){
			System.out.println("There was an error setting the article!" + e.getMessage());
		}
		return false;
	}
	
	
	private void createArticle(){
		try {
			_article = ArticleFactory.newArticle(_rs.getString("article_type"));
		} catch (SQLException e) {
			System.out.println("Could not create Article!");
		} 
	}
	
	public void SaveBook(Book book){
		String stmt = null;
		stmt = "INSERT INTO articles (Users_user_id, article_title, article_type, article_description, article_year, article_genre, article_b_author, article_b_publisher, article_b_pages, article_auction_start, article_auction_end, article_auction_price, article_auction_type) values (" 
				+ book.get_user_id() +"," 
				+ book.get_title() + ","
				+ book.get_articleType() + ", "
				+ book.get_description() + ", "
				+ book.get_year() + ", "
				+ book.get_genre() + ", "
				+ book.get_author() + ", "
				+ book.get_publisher() + ", "
				+ book.get_pages() + ", "
				+ book.get_start() + ", "
				+ book.get_end() + ", "
				+ book.get_price() + ", "
				+ book.get_auctionType() +")";
		_db.ExecuteUpdate(stmt);		
	}
	public void SaveCD(CD cd){
		String stmt = null;
		stmt = "";
		_db.ExecuteUpdate(stmt);		
	}
	public void SaveDVD(DVD dvd){
		String stmt = null;
		stmt = "";
		_db.ExecuteUpdate(stmt);		
	}
	public void DeleteArticle(int article_id){}
}